﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using CardInChina.Data.Entity.Entity;
using System.Collections;


namespace CardInChina.Data.Entity.Interface
{
    public class OrderService
    {
        /// <summary>
        /// return order index
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public string InsertOneOrder(Order order)
        {
            if (order == null) throw new ArgumentNullException("order");
            using (CICEntities db = new CICEntities())
            {
                Order _order = db.Orders.Add(order);
                db.SaveChanges();
                return _order.OrdersID.ToString();
            }
        }

        public Order GetOrderByOrderID(string orderID)
        {
            if (string.IsNullOrEmpty(orderID))
                return null;
            int targetId = int.Parse(orderID);
            using (CICEntities db = new CICEntities())
            {
                return db.Orders.FirstOrDefault(order => order.OrdersID == targetId);
            }

        }

        public IEnumerable GetOrdersByCustomer(int uid)
        {
            using (CICEntities db = new CICEntities())
            {
                var sql = from t in db.Orders
                          where t.CustomerID == uid
                          orderby t.OrdersID descending
                          select new
                          {
                              t.OrdersID,
                              t.TotalPrice,
                              t.OfCard.OfCardInfo.CardName,
                              t.OfCard.OfCardInfo.CardKey,
                              t.OfCard.CardMemo,
                              t.OfCard.Photo,
                              t.OrderCreateTime,
                              t.PayStatus,
                          };


                return sql.ToArray();
            }
        }


    }
}
